package com.leoman.controller.backend;

import com.leoman.common.DataTableFactory;
import com.leoman.controller.common.CommonController;
import com.leoman.core.Constant;
import com.leoman.entity.Video;
import com.leoman.entity.vo.RegionVo;
import com.leoman.service.VideoRecordService;
import com.leoman.service.VideoService;
import com.leoman.utils.DateUtils;
import com.leoman.utils.JsonUtil;
import com.leoman.utils.WebUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import java.util.Map;

/**
 * 视频管理
 * Created by 涂奕恒 on 2016/11/02.
 */
@Controller
@RequestMapping(value = "backend/video")
public class VideoController extends CommonController {

    @Autowired
    private VideoService videoService;

    @Autowired
    private VideoRecordService videoRecordService;

    @RequestMapping("index")
    public String index() {
        return "backend/视频列表";
    }

    @RequestMapping("/list")
    public void list(HttpServletResponse response,
                     String name,
                     String nickName,
                     Integer videoTypeId,
                     Integer sendType,
                     Integer status,
                     Integer sortType,
                     Integer draw,
                     Integer start,
                     Integer length) {
        RegionVo regionVo;

        if (null == start || start == 0) {
            start = 1;
        }
        int pageNum = getPageNum(start, length);
        Page<Video> page = videoService.pageByParams(null, name, nickName, videoTypeId, sendType, status, sortType, pageNum, length);

        for (Video video : page.getContent()) {
            regionVo = videoService.getVideoStatus(video);
            video.setTempStatus(regionVo.getId());
            video.setTempContent(regionVo.getName());
        }

        Map<String, Object> result = DataTableFactory.fitting(draw, page);
        WebUtil.printJson(response, result);
    }

    @RequestMapping("show")
    public String show(ModelMap model, Integer videoId) {
        if (null != videoId) {
            Video video = videoService.getById(videoId);
            model.addAttribute("video", video);

            // 查询该视频的投放记录
            model.addAttribute("videoRecordList", videoRecordService.findListByVideoId(videoId));
        }

        return "backend/视频详情";
    }

    /**
     * 审核视频
     *
     * @param videoId 视频ID
     * @param status  状态
     * @return
     */
    @RequestMapping("/changeCheckStatus")
    @ResponseBody
    public Integer changeCheckStatus(Integer videoId, Integer status) {
        try {
            if (status.equals(Constant.CHECK_STATUS_DEFAULT)) {
                return videoService.changeVideoShopping(videoId, status);
            } else {
                Integer[] postIds = new Integer[1];
                postIds[0] = videoId;
                return videoService.changeVideoCheckStatus(postIds, status);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 0;
    }

    /**
     * 批量删除视频
     *
     * @param ids 视频ID
     * @return
     */
    @RequestMapping("/batchDelete")
    @ResponseBody
    public Integer batchDelete(String ids) {
        try {
            Integer[] videoIds = JsonUtil.json2Obj(ids, Integer[].class);
            return videoService.batchDelete(videoIds);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 0;
    }

    /**
     * 删除视频
     *
     * @param videoId 视频ID
     * @return
     */
    @RequestMapping("/delete")
    @ResponseBody
    public Integer delete(Integer videoId) {
        try {
            return videoService.deleteByVideoId(videoId);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 0;
    }

    @RequestMapping("saveRecord")
    @ResponseBody
    public Integer saveRecord(Integer videoId, String startTime, String endTime) {
        try {
            videoService.iSendVideo(videoId, Constant.VIDEO_SEND_TYPE_FREE, "", "", DateUtils.stringToDateWithFormat(startTime, "yyyy-MM-dd hh:mm").getTime(), DateUtils.stringToDateWithFormat(endTime, "yyyy-MM-dd hh:mm").getTime(), 0.0, 0, Constant.CHECK_STATUS_DEFAULT, Constant.CHECK_STATUS_SUCCESS);
            return 1;
        } catch (Exception e) {
            e.printStackTrace();
            return 0;
        }
    }
}
